//
//  HYCSRViewController.m
//  FamilyTravel
//
//  Created by dllo on 15/11/2.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "HYCSRViewController.h"
#import "HYCHeadDocument.h"

@interface HYCSRViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>
@property(nonatomic, retain)UISearchBar *searchBar;
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSArray *myArr;
@property(nonatomic, retain)NSString *tableViewStr;
@property(nonatomic, retain) NSDictionary *dic;

@end


@implementation HYCSRViewController
- (void)dealloc{
    [_dic release];
    [_searchBar release];
    [_tableView release];
    [_myArr release];
    [_tableViewStr release];
    
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self createData];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [_tableView release];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    self.tableView.alpha = 0.5;
    
}


- (void)createData{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    NSString *str = [NSString stringWithFormat:@"%@%@%@",@"http://open.qyer.com/qyer/place/autocomplate?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&keyword=", self.resStr,@"&mode=city&page=1&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=AB5B28ED-8670-4190-873A-F09EFE3EB3E8&track_os=ios%25207.1.2&v=1"];
    
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.dic = responseObject;
        self.myArr = self.dic[@"data"][@"entry"];
        
        [self.tableView reloadData];

        if (self.myArr.count == 0) {
            
            UIAlertView *firstAltView = [[UIAlertView alloc] initWithTitle:@"哥们" message:@"你这个城市名不对啊" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
             [firstAltView show];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
 
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.myArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    if (self.myArr.count != 0) {
        cell.textLabel.text = self.myArr[indexPath.row][@"cnname"];
    }

    
    
    
    // 关键字高亮
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:cell.textLabel.text];
    [attribute addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[cell.textLabel.text rangeOfString:self.gStr]];
    [cell.textLabel setAttributedText:attribute];
    
    
//    NSLog(@"######%@", self.gStr);
 
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HYCPudController *pCVC = [[HYCPudController alloc] init];
    
    pCVC.str = self.myArr[indexPath.row][@"id"];
    [self.navigationController pushViewController:pCVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end